<?php
/*
COMFYPAGE SCRIPT by CAMERON DAVIS for comfypage.com
This script makes it easy to create, edit and delete the files (HTML, CSS, Javascript etc.) that make up your website.
Website: http://comfypage.com/comfypage_script/

COPYRIGHT
Copyright 2011 Cameron Davis (cameronjohndavis@gmail.com)

INSTALLATION
1) Download the script. Name it whatever you like.
2) Place it in your public WWW directory.
3) Password protect it and ComfyPage Script's backup file so only you can access them. E.g. Use .htaccess.
4) Point your browser at the uploaded script.

LICENSE (GNU GPL v3)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
//list of file extensions listed for editing and included in backup file generation
$file_extensions = array('html','htm','css','js', 'xml', 'txt');
//list of files worthy of highlighting. E.g. index.html
$highlight_files = array('index', 'rss', 'style');
$file = '';
$content = '';
//if a save was made there will be POST data
if(!empty($_POST['file']))
{
	$file = $_POST['file'];
	$content = $_POST['content'];
	file_put_contents($file, $content);
}
//if a file was selected
else if(!empty($_GET['file']))
{
	$file = $_GET['file'];
	if(file_exists($file))	$content = file_get_contents($file);
}
//if a file was deleted
if(!empty($_POST['delete']) && file_exists($_POST['delete']))
{
	unlink($_POST['delete']);
}

$current_script = $_SERVER['SCRIPT_NAME'];
$break = Explode('/', $current_script);
$current_script = $break[count($break) - 1]; 
//load the file list from current dir
$file_list = get_file_list($current_script);

//if a backup was requested
if(!empty($_POST['backup']))
{
	//get web address of current site
	$root_dir = $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI'];
	//remove everything after the last slash
	$root_dir = substr($root_dir, 0, strrpos($root_dir, '/') + 1);
	$all_content = 'Backup of '.$root_dir.' created '.date('F jS Y').'. '. count($file_list) . ' file(s) in backup.';
	$all_content .= "\n=====================================================\n";
	foreach($file_list as $file_list_item)
	{
		$all_content .= $file_list_item . "\n";
		$all_content .= file_get_contents($file_list_item);
		$all_content .= "\n=====================================================\n";
	}
	file_put_contents('website_backup.txt', $all_content);
}

//defaults if no files exist
if(count($file_list) == 0)
{
	$file = 'index.html';
	$content = <<<EOD
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;My Website&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;h1&gt;Welcome to My Website&lt;/h1&gt;
		&lt;p&gt;This editor makes it simple to create, edit and delete the text files (HTML, CSS, Javascript etc.) that make up your website. Clicking "Save" right now will write index.html to the current directory with this content. Likewise, you can create, edit and delete all the text files that make up your website.&lt;/p&gt;
		&lt;p&gt;I recommend hosting images and files on third-party services like Flickr, Picasa and Google Docs.&lt;/p&gt;
		&lt;p&gt;Make sure the editor ($current_script) is password protected so only you can access it.&lt;/p&gt;
	&lt;/body&gt;
&lt;/html&gt;
EOD;

}

//the URL of the file being edited
$dir = dirname($_SERVER['PHP_SELF']);
$dir = str_replace('\\','',$dir); //get rid of slashes
$dir .= '/'; //add proper slash
$file_url = 'http://'.$_SERVER['HTTP_HOST'].$dir.$file;

//get list of text files
//$current_script name of currently executing script
function get_file_list($current_script)
{
	global $file_extensions;
	$file_list = array();
	$handle = opendir('.');
	$tmp = null;
	while(($file_handle = readdir($handle)) !== false)
	{
		//if it's a special file
		if(is_dir($file_handle) || ($file_handle == $current_script || $file_handle == 'website_backup.txt'))
		{
		    //don't add it
			continue;
		}
	    $tmp = pathinfo($file_handle);
	    $tmp = strtolower($tmp['extension']);
	    //if a valid file extension
	    if(in_array($tmp, $file_extensions))
	    {
			//add to the list
			$file_list[] = $file_handle;
		}
	}
	closedir($handle);
	sort($file_list);
	return $file_list;
}

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title><?php echo($file); ?></title>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
		<STYLE TYPE="text/css" MEDIA=screen>
<!--
  BODY  { font-family : monospace;}
-->
		</STYLE>
	</head>
	<body>
		<form method="POST" action="#">
			<div>
				<input type="submit" value="Save" tabindex="3">
				<input name="file" value="<?php echo($file); ?>" tabindex="1">
				<a href="<?php echo($file); ?>" tabindex="4">View</a>
				&nbsp;&nbsp;&nbsp;&nbsp;
				<?php
				foreach($file_list as $file_list_item)
				{
					$tmp = pathinfo($file_list_item);
				    $tmp = strtolower($tmp['filename']);
				    //if a valid file extension
				    if(in_array($tmp, $highlight_files))
				    {
						echo "<a href=\"?file=$file_list_item\">$file_list_item</a>&nbsp; ";
					}
				}
				?>
			</div>
			<div><textarea style="width:100%" name="content" rows="30" cols="100" tabindex="2"><?php echo($content); ?></textarea></div>
			<div style="text-align:right;">
				<a target="_blank" href="http://validator.w3.org/check?uri=<?php echo($file_url); ?>" tabindex="6">Validate HTML</a>
				<a target="_blank" href="http://jigsaw.w3.org/css-validator/validator?uri=<?php echo($file_url); ?>" tabindex="6">Validate CSS</a>
				<a target="_blank" href="http://validator.w3.org/checklink?uri=<?php echo($file_url); ?>" tabindex="6">Link checker</a>
			</div>
		</form>
		<form method="POST" action="#">
			<div style="text-align:right;">
				<input type="submit" value="Delete" tabindex="5">
				<input name="delete" readonly value="<?php echo($file); ?>">
			</div>
		</form>
		<div style='padding-bottom:0.5em;padding-top:0.5em;'>
			<?php
			foreach($file_list as $file_list_item)
			{
				echo "<a href=\"?file=$file_list_item\">$file_list_item</a>&nbsp; ";
			}
			?>
		</div>
		<div style="text-align:center">
			<form method="POST" action="#">
				<div>
					<input type="submit" value="Re-generate backup file" name="backup">
					<?php if(file_exists('website_backup.txt')) echo('<a href="website_backup.txt">Download backup</a>') ?>
				</div>
			</form>
			<p>
				<a href="http://htmlhelp.com/reference/html40/">HTML Reference</a>
				<a href="http://htmlhelp.com/reference/css/">CSS Reference</a>
			</p>
			<p>&copy; 2012 Cameron Davis. This script is <a href="http://comfypage.com/comfypage_script.htm">Powered by ComfyPage</a></p>
		</div>
	</body>
</html>